package transaction;

import java.sql.*;
import java.util.Arrays;

public class TransactionTest {
	public static void main(String[] args) {
		System.out.println("kekekek");
		try {
			// load driver
			// Class.forName("net.sourceforge.jtds.jdbc.Driver");
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			System.out.println("driver da duoc load ");
			// tao ket noi den CSDL
			// Connection connection = DriverManager.getConnection(
			// "jdbc:jtds:sqlserver://localhost:1433/test", "sa",
			// "sa");
			Connection connection = DriverManager
					.getConnection("jdbc:sqlserver://localhost:1433;databaseName=test;user=sa;password=sa");
			System.out.println("Da ket noi duoc voi CSDL");

			//---------------------------------------------
			connection.setAutoCommit(false);
			// thuc hien cau truy van
			Statement state = connection
					.createStatement();
			
			//tao Save point truoc khi thuc thi
			Savepoint sp = connection.setSavepoint("sp1");
						
			state.executeUpdate("INSERT INTO SINHVIEN VALUES (101, 'Nguyen van ty')");
			
			//
			connection.commit();
			
			//co loi ERROR thi rollback lai
			connection.rollback(sp);

		} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
		} catch (SQLException ex) {
			ex.printStackTrace();
		}

	}
}
